import os
import pandas as pd
from API.currency import get_currency_latest_price
from API.okex import get_usdtcny_premium_okex, get_usdtcny_orderbook_okex
from utils import df_into_db

# 从okex获取usdt对cny溢价数据和盘口信息，每小时更新
DATA_DIR = 'E:\\data'
PREMIUM_DATA_DIR = os.path.join(DATA_DIR, 'usdt_premium')
print('更新usdt对cny溢价数据和盘口信息')
file_name = os.path.join(PREMIUM_DATA_DIR, f'usdtcny_premium_okex.csv')


last_usdcnh = get_currency_latest_price(symbol='USDCNH')
premium = get_usdtcny_premium_okex()
premium['usdcnh'] = last_usdcnh
premium['premium'] = (premium['usdtcny'] - last_usdcnh) / last_usdcnh
if os.path.exists(file_name):
    historical_premium = pd.read_csv(file_name)
    df = pd.concat([historical_premium, premium], axis=0, ignore_index=True)
    df.drop_duplicates(subset=['datetime'], keep='first', inplace=True)
    df_into_db(df, db_name='usdt_premium', table_name='usdtcny_premium_okex')
    # df.to_csv(file_name, index=False)
else:
    # premium.to_csv(file_name, index=False)
    pass
